using UnityEngine;
using System.Collections;

public class SmelterManager : WorkshopManager {

	// Use this for initialization
	void Start () {
		queue = new ArrayList();
	}
	
	// Update is called once per frame
	void Update () {
		if (queue.Count > 0){
			Job j = (Job)queue[0];
			if (j.percentageComplete > 100){
				queue.RemoveAt(0);
				if (queue.Count > 0){
					Controller.addJob((Job)queue[0]);
				}
			}
		}
	}
	
	public void OnMouseUp(){
		queueJob();
	}
	
	public void queueJob(){
		clicked = true;
	}
	
	void OnGUI(){
		if (clicked){
			GUI.Box(new Rect(10, 10, 180, 230), "Smelter");
			
			if (queue.Count != 0){	
				GUI.Box(new Rect(10, 245, 180, 23 * queue.Count + 25), "Queue");
				int i = 0;
				
				ArrayList toBeRemoved = new ArrayList();
				
				foreach (Job job in queue){
					
					if (GUI.Button(new Rect(15, 270 + 23 * i, 160, 20), (string) job.jobLabel)){
						toBeRemoved.Add(job);
					}
					i++;
				}
				
				foreach (Job job in toBeRemoved){
					queue.Remove(job);
				}
				
			}
			
			try {
				if (GUI.Button (new Rect (159, 12, 25, 20), "X")){
					clicked = false;
				} else if (GUI.Button (new Rect (20, 40, 160, 20), "Smelt Iron")) {
					clicked = false;
					queue.Add(new SmeltOre((int)transform.position.x, (int)transform.position.y, (int)transform.position.z, (Ore) (ItemList.ironOre[0])));
				} else if (GUI.Button (new Rect (20, 62, 160, 20), "Smelt Tin")) {
					clicked = false;
					queue.Add(new SmeltOre((int)transform.position.x, (int)transform.position.y, (int)transform.position.z, (Ore) (ItemList.tinOre[0])));
				} else if (GUI.Button (new Rect (20, 84, 160, 20), "Smelt Aluminum")) {
					clicked = false;
					queue.Add(new SmeltOre((int)transform.position.x, (int)transform.position.y, (int)transform.position.z, (Ore) (ItemList.aluminumOre[0])));
				} else if (GUI.Button (new Rect (20, 106, 160, 20), "Smelt Copper")) {
					clicked = false;
					queue.Add(new SmeltOre((int)transform.position.x, (int)transform.position.y, (int)transform.position.z, (Ore) (ItemList.copperOre[0])));
				} else if (GUI.Button (new Rect (20, 128, 160, 20), "Smelt Zinc")) {
					clicked = false;
					queue.Add(new SmeltOre((int)transform.position.x, (int)transform.position.y, (int)transform.position.z, (Ore) (ItemList.zincOre[0])));
				} else if (GUI.Button (new Rect (20, 150, 160, 20), "Smelt Brass")) {
					clicked = false;
					queue.Add(new SmeltBrass((int)transform.position.x, (int)transform.position.y, (int)transform.position.z, (Bar) ItemList.zincBars[0], (Bar) (ItemList.copperBars[0])));
				} else if (GUI.Button (new Rect (20, 172, 160, 20), "Smelt Bronze")) {
					clicked = false;
					queue.Add(new SmeltBronze((int)transform.position.x, (int)transform.position.y, (int)transform.position.z, (Bar) (ItemList.tinBars[0]), (Bar) (ItemList.copperBars[0])));
				} else if (GUI.Button (new Rect (20, 194, 160, 20), "Smelt Steel")) {
					clicked = false;
					queue.Add(new SmeltSteel((int)transform.position.x, (int)transform.position.y, (int)transform.position.z));
				} else if (GUI.Button (new Rect (20, 216, 160, 20), "Galvanize Steel")) {
					clicked = false;
					queue.Add(new SmeltGalvanizedSteel((int)transform.position.x, (int)transform.position.y, (int)transform.position.z, (Bar) ItemList.zincBars[0], (Bar) (ItemList.steelBars[0])));
				}
				if (queue.Count > 1){
					try {
						Controller.removeJob((Job)queue[queue.Count - 1]);
					} catch {
						
					}
				}
			} catch {
				Debug.Log("No ore");
			}
		}
	}
}
